package com.lineage.server.model.Instance;

import com.lineage.config.ConfigAlt;
import com.lineage.server.datatables.NPCTalkDataTable;
import com.lineage.server.model.L1AttackPc;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1NpcTalkData;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.types.Point;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1GuardInstance extends L1NpcInstance {
  private static final long serialVersionUID = 1L;
  
  private static final Log _log = LogFactory.getLog(L1GuardInstance.class);
  
  public L1GuardInstance(L1Npc template) {
    super(template);
  }
  
  private static L1PcInstance searchTarget(L1GuardInstance npc) {
    L1PcInstance targetPlayer = null;
    Iterator<L1PcInstance> iterator = World.get().getVisiblePlayer((L1Object)npc).iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc = iterator.next();
      try {
        Thread.sleep(10L);
      } catch (InterruptedException e) {
        _log.error(e.getLocalizedMessage(), e);
      } 
      if (pc.getCurrentHp() > 0 && !pc.isDead() && !pc.isGm() && !pc.isGhost() && 
        npc.get_showId() == pc.get_showId() && (!pc.isInvisble() || npc.getNpcTemplate().is_agrocoi()) && 
        pc.isWanted()) {
        targetPlayer = pc;
        break;
      } 
    } 
    return targetPlayer;
  }
  
  public void searchTarget() {
    L1PcInstance targetPlayer = searchTarget(this);
    if (targetPlayer != null) {
      this._hateList.add(targetPlayer, 0);
      this._target = targetPlayer;
    } 
  }
  
  public void setTarget(L1PcInstance targetPlayer) {
    if (targetPlayer != null) {
      this._hateList.add(targetPlayer, 0);
      this._target = targetPlayer;
    } 
  }
  
  public boolean noTarget() {
    if (getLocation().getTileLineDistance(new Point(getHomeX(), getHomeY())) > 0) {
      if (this._npcMove != null) {
        int dir = this._npcMove.moveDirection(getHomeX(), getHomeY());
        if (dir != -1) {
          this._npcMove.setDirectionMove(dir);
          setSleepTime(calcSleepTime(getPassispeed(), 0));
        } else {
          teleport(getHomeX(), getHomeY(), 1);
        } 
      } 
    } else if (World.get().getRecognizePlayer((L1Object)this).size() == 0) {
      return true;
    } 
    return false;
  }
  
  public void onNpcAI() {
    if (isAiRunning())
      return; 
    setActived(false);
    startAI();
  }
  
  public void onAction(L1PcInstance pc) {
    try {
      if (!isDead())
        if (getCurrentHp() > 0) {
          L1AttackPc l1AttackPc = new L1AttackPc(pc, this);
          if (l1AttackPc.calcHit())
            l1AttackPc.calcDamage(); 
          l1AttackPc.action();
          l1AttackPc.commit();
        } else {
          L1AttackPc l1AttackPc = new L1AttackPc(pc, this);
          l1AttackPc.calcHit();
          l1AttackPc.action();
        }  
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void onTalkAction(L1PcInstance player) {
    int objid = getId();
    L1NpcTalkData talking = NPCTalkDataTable.get().getTemplate(getNpcTemplate().get_npcId());
    int npcid = getNpcTemplate().get_npcId();
    String htmlid = null;
    String[] htmldata = null;
    boolean hascastle = false;
    String clan_name = " ";
    String pri_name = " ";
    if (talking != null) {
      if (npcid == 70549 || npcid == 70985) {
        hascastle = checkHasCastle(player, 1);
        if (hascastle) {
          htmlid = "gateokeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 70656) {
        hascastle = checkHasCastle(player, 1);
        if (hascastle) {
          htmlid = "gatekeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 70600 || npcid == 70986) {
        hascastle = checkHasCastle(player, 2);
        if (hascastle) {
          htmlid = "orckeeper";
        } else {
          htmlid = "orckeeperop";
        } 
      } else if (npcid == 70687 || npcid == 70987) {
        hascastle = checkHasCastle(player, 3);
        if (hascastle) {
          htmlid = "gateokeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 70778) {
        hascastle = checkHasCastle(player, 3);
        if (hascastle) {
          htmlid = "gatekeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 70800 || npcid == 70988 || npcid == 70989 || npcid == 70990 || npcid == 70991) {
        hascastle = checkHasCastle(player, 4);
        if (hascastle) {
          htmlid = "gateokeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 70817) {
        hascastle = checkHasCastle(player, 4);
        if (hascastle) {
          htmlid = "gatekeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 70862 || npcid == 70992) {
        hascastle = checkHasCastle(player, 5);
        if (hascastle) {
          htmlid = "gateokeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 70863) {
        hascastle = checkHasCastle(player, 5);
        if (hascastle) {
          htmlid = "gatekeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 70993 || npcid == 70994) {
        hascastle = checkHasCastle(player, 6);
        if (hascastle) {
          htmlid = "gateokeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 70995) {
        hascastle = checkHasCastle(player, 6);
        if (hascastle) {
          htmlid = "gatekeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 70996) {
        hascastle = checkHasCastle(player, 7);
        if (hascastle) {
          htmlid = "gatekeeper";
          htmldata = new String[] { player.getName() };
        } else {
          htmlid = "gatekeeperop";
        } 
      } else if (npcid == 60514) {
        Collection allClans = WorldClan.get().getAllClans();
        Iterator<L1Clan> iter = allClans.iterator();
        while (iter.hasNext()) {
          L1Clan clan = iter.next();
          if (clan.getCastleId() == 1) {
            clan_name = clan.getClanName();
            pri_name = clan.getLeaderName();
            break;
          } 
        } 
        htmlid = "ktguard6";
        htmldata = new String[] { getName(), clan_name, pri_name };
      } else if (npcid == 60560) {
        Collection allClans = WorldClan.get().getAllClans();
        Iterator<L1Clan> iter = allClans.iterator();
        while (iter.hasNext()) {
          L1Clan clan = iter.next();
          if (clan.getCastleId() == 2) {
            clan_name = clan.getClanName();
            pri_name = clan.getLeaderName();
            break;
          } 
        } 
        htmlid = "orcguard6";
        htmldata = new String[] { getName(), clan_name, pri_name };
      } else if (npcid == 60552) {
        Collection allClans = WorldClan.get().getAllClans();
        Iterator<L1Clan> iter = allClans.iterator();
        while (iter.hasNext()) {
          L1Clan clan = iter.next();
          if (clan.getCastleId() == 3) {
            clan_name = clan.getClanName();
            pri_name = clan.getLeaderName();
            break;
          } 
        } 
        htmlid = "wdguard6";
        htmldata = new String[] { getName(), clan_name, pri_name };
      } else if (npcid == 60524 || npcid == 60525 || npcid == 60529) {
        Collection allClans = WorldClan.get().getAllClans();
        Iterator<L1Clan> iter = allClans.iterator();
        while (iter.hasNext()) {
          L1Clan clan = iter.next();
          if (clan.getCastleId() == 4) {
            clan_name = clan.getClanName();
            pri_name = clan.getLeaderName();
            break;
          } 
        } 
        htmlid = "grguard6";
        htmldata = new String[] { getName(), clan_name, pri_name };
      } else if (npcid == 70857) {
        Collection allClans = WorldClan.get().getAllClans();
        Iterator<L1Clan> iter = allClans.iterator();
        while (iter.hasNext()) {
          L1Clan clan = iter.next();
          if (clan.getCastleId() == 5) {
            clan_name = clan.getClanName();
            pri_name = clan.getLeaderName();
            break;
          } 
        } 
        htmlid = "heguard6";
        htmldata = new String[] { getName(), clan_name, pri_name };
      } else if (npcid == 60530 || npcid == 60531) {
        Collection allClans = WorldClan.get().getAllClans();
        Iterator<L1Clan> iter = allClans.iterator();
        while (iter.hasNext()) {
          L1Clan clan = iter.next();
          if (clan.getCastleId() == 6) {
            clan_name = clan.getClanName();
            pri_name = clan.getLeaderName();
            break;
          } 
        } 
        htmlid = "dcguard6";
        htmldata = new String[] { getName(), clan_name, pri_name };
      } else if (npcid == 60533 || npcid == 60534) {
        Collection allClans = WorldClan.get().getAllClans();
        Iterator<L1Clan> iter = allClans.iterator();
        while (iter.hasNext()) {
          L1Clan clan = iter.next();
          if (clan.getCastleId() == 7) {
            clan_name = clan.getClanName();
            pri_name = clan.getLeaderName();
            break;
          } 
        } 
        htmlid = "adguard6";
        htmldata = new String[] { getName(), clan_name, pri_name };
      } else if (npcid == 81156) {
        Collection allClans = WorldClan.get().getAllClans();
        Iterator<L1Clan> iter = allClans.iterator();
        while (iter.hasNext()) {
          L1Clan clan = iter.next();
          if (clan.getCastleId() == 8) {
            clan_name = clan.getClanName();
            pri_name = clan.getLeaderName();
            break;
          } 
        } 
        htmlid = "ktguard6";
        htmldata = new String[] { getName(), clan_name, pri_name };
      } 
      if (htmlid != null) {
        if (htmldata != null) {
          player.sendPackets((ServerBasePacket)new S_NPCTalkReturn(objid, htmlid, htmldata));
        } else {
          player.sendPackets((ServerBasePacket)new S_NPCTalkReturn(objid, htmlid));
        } 
      } else if (player.getLawful() < -1000) {
        player.sendPackets((ServerBasePacket)new S_NPCTalkReturn(talking, objid, 2));
      } else {
        player.sendPackets((ServerBasePacket)new S_NPCTalkReturn(talking, objid, 1));
      } 
    } 
  }
  
  public void onFinalAction() {}
  
  public void doFinalAction() {}
  
  public void receiveDamage(L1Character attacker, int damage) {
    this.ISASCAPE = false;
    if (getCurrentHp() > 0 && !isDead()) {
      if (damage >= 0 && !(attacker instanceof L1EffectInstance))
        if (attacker instanceof L1IllusoryInstance) {
          L1IllusoryInstance ill = (L1IllusoryInstance)attacker;
          attacker = ill.getMaster();
          setHate(attacker, damage);
        } else {
          setHate(attacker, damage);
        }  
      if (damage > 0) {
        removeSkillEffect(66);
        removeSkillEffect(212);
      } 
      onNpcAI();
      if (attacker instanceof L1PcInstance && damage > 0) {
        L1PcInstance pc = (L1PcInstance)attacker;
        pc.setPetTarget(this);
      } 
      int newHp = getCurrentHp() - damage;
      if (newHp <= 0 && !isDead()) {
        setCurrentHpDirect(0);
        setDead(true);
        setStatus(8);
        Death death = new Death(attacker);
        GeneralThreadPool.get().execute(death);
      } 
      if (newHp > 0)
        setCurrentHp(newHp); 
    } else if ((getCurrentHp() != 0 || isDead()) && !isDead()) {
      setDead(true);
      setStatus(8);
      Death death2 = new Death(attacker);
      GeneralThreadPool.get().execute(death2);
    } 
  }
  
  public void setCurrentHp(int i) {
    int currentHp = Math.min(i, getMaxHp());
    if (getCurrentHp() == currentHp)
      return; 
    setCurrentHpDirect(currentHp);
  }
  
  private boolean checkHasCastle(L1PcInstance pc, int castleId) {
    boolean isExistDefenseClan = false;
    Collection allClans = WorldClan.get().getAllClans();
    Iterator<L1Clan> iter = allClans.iterator();
    while (iter.hasNext()) {
      L1Clan clan = iter.next();
      if (castleId == clan.getCastleId()) {
        isExistDefenseClan = true;
        break;
      } 
    } 
    if (!isExistDefenseClan)
      return true; 
    if (pc.getClanid() != 0) {
      L1Clan clan2 = WorldClan.get().getClan(pc.getClanname());
      if (clan2 != null && clan2.getCastleId() == castleId)
        return true; 
    } 
    return false;
  }
  
  class Death implements Runnable {
    L1Character _lastAttacker;
    
    public Death(L1Character lastAttacker) {
      this._lastAttacker = lastAttacker;
    }
    
    public void run() {
      L1GuardInstance.this.setDeathProcessing(true);
      L1GuardInstance.this.setCurrentHpDirect(0);
      L1GuardInstance.this.setDead(true);
      L1GuardInstance.this.setStatus(8);
      L1GuardInstance.this.getMap().setPassable((Point)L1GuardInstance.this.getLocation(), true);
      L1GuardInstance.this.broadcastPacketAll((ServerBasePacket)new S_DoActionGFX(L1GuardInstance.this.getId(), 8));
      L1GuardInstance.this.startChat(1);
      L1GuardInstance.this.setDeathProcessing(false);
      L1GuardInstance.this.allTargetClear();
      L1GuardInstance.this.startDeleteTimer(ConfigAlt.NPC_DELETION_TIME);
    }
  }
}
